package com.pureut.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.security.utils.SecurityUtils;
import com.pureut.system.api.FeignService;
import com.pureut.system.api.domain.SysDept;
import com.pureut.system.api.domain.SysUser;
import com.pureut.system.domain.MaterialCertificationApplication;
import com.pureut.system.domain.MaterialCertificationDetail;
import com.pureut.system.domain.SysAuditHistoryTest;
import com.pureut.system.domain.dto.MaterialCertificationApplicationDetailsDto;
import com.pureut.system.domain.dto.MaterialCertificationApplicationInitDto;
import com.pureut.system.domain.dto.MaterialCertificationApplicationPagesDto;
import com.pureut.system.domain.vo.MaterialCertificationApplicationVo;
import com.pureut.system.mapper.MaterialCertificationApplicationMapper;
import com.pureut.system.mapper.SysAuditHistoryTestMapper;
import com.pureut.system.service.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Author: pureut
 * @Date: 2024-12-04 18:23:47
 * @Desc: 物料认证申请
 */
@Service("materialCertificationApplicationService")
public class MaterialCertificationApplicationServiceImpl extends ServiceImpl<MaterialCertificationApplicationMapper, MaterialCertificationApplication> implements IMaterialCertificationApplicationService {

    /**
     * 物料认证申请Dao
     */
    @Resource
    private MaterialCertificationApplicationMapper materialCertificationApplicationMapper;

    @Resource
    private IMaterialAwmateriaService materialAwmateriaService;

    /**
     * 物料认证明细
     */
    @Resource
    private IMaterialCertificationDetailService materialCertificationDetailService;

    @Resource
    FeignService feignService;

    @Resource
    ISysDeptService sysDeptService;
    @Resource
    ISysUserService sysUserService;

    /**
     * 审核历史  测试
     */
    @Resource
    SysAuditHistoryTestMapper sysAuditHistoryTestMapper;

    /**
     * 查询列表
     *
     * @param materialCertificationApplicationVo
     * @return
     */
    @Override
    public List<MaterialCertificationApplicationPagesDto> queryPage(MaterialCertificationApplicationVo materialCertificationApplicationVo) {
        List<MaterialCertificationApplicationPagesDto> materialCertificationApplicationPagesDtos = materialCertificationApplicationMapper.selecByPage(materialCertificationApplicationVo);

        return materialCertificationApplicationPagesDtos;
    }


    /**
     * 新增物料认证申请
     *
     * @param materialCertificationApplicationVo
     * @return
     */
    @Transactional
    @Override
    public Boolean save(MaterialCertificationApplicationVo materialCertificationApplicationVo) {
        MaterialCertificationApplication materialCertificationApplication = new MaterialCertificationApplication();
        materialCertificationApplicationVo.setProposerBy(null);
        materialCertificationApplicationVo.setApplicationDept(null);
        BeanUtil.copyProperties(materialCertificationApplicationVo, materialCertificationApplication);
        materialCertificationApplication.setCreateBy(SecurityUtils.getUsername());
        materialCertificationApplication.setCreateTime(new Date());
        materialCertificationApplication.setApplicationDept(SecurityUtils.getDeptId());
        boolean save = save(materialCertificationApplication);
        if (save) {
            if (ObjectUtil.isNotEmpty(materialCertificationApplicationVo.getMaterialCertificationDetailList())) {
                List<MaterialCertificationDetail> collect = materialCertificationApplicationVo.getMaterialCertificationDetailList().stream().map(e -> {
                    MaterialCertificationDetail materialCertificationDetail = new MaterialCertificationDetail();
                    materialCertificationDetail.setCreateBy(SecurityUtils.getUsername());
                    materialCertificationDetail.setCreateTime(new Date());
                    materialCertificationDetail.setMaterialCertificationId(materialCertificationApplication.getId());
                    materialCertificationDetail.setMinPurchaseQuantity(e.getMinPurchaseQuantity());
                    materialCertificationDetail.setMaterielId(e.getMaterielId());
                    materialCertificationDetail.setSupplierCode(e.getSupplierCode());
                    materialCertificationDetail.setSupplierName(e.getSupplierName());
                    return materialCertificationDetail;
                }).collect(Collectors.toList());
                materialCertificationDetailService.saveBatch(collect);
            }
        }
        return save;
    }


    /**
     * 更新物料认证申请
     *
     * @param materialCertificationApplicationVo
     * @return
     */
    @Transactional
    @Override
    public Boolean updateById(MaterialCertificationApplicationVo materialCertificationApplicationVo) {
        MaterialCertificationApplication materialCertificationApplication = new MaterialCertificationApplication();
        BeanUtil.copyProperties(materialCertificationApplicationVo, materialCertificationApplication);
        materialCertificationApplication.setUpdateBy(SecurityUtils.getUsername());
        materialCertificationApplication.setUpdateTime(new Date());
        materialCertificationApplication.setApplicationDept(SecurityUtils.getDeptId());
        boolean b = updateById(materialCertificationApplication);
        if(b){
            Map<String,Object> map=new HashMap<>();
            map.put("material_certification_id",materialCertificationApplicationVo.getId());
            materialCertificationDetailService.removeByMap(map);
            if (ObjectUtil.isNotEmpty(materialCertificationApplicationVo.getMaterialCertificationDetailList())) {
                List<MaterialCertificationDetail> collect = materialCertificationApplicationVo.getMaterialCertificationDetailList().stream().map(e -> {
                    MaterialCertificationDetail materialCertificationDetail = new MaterialCertificationDetail();
                    materialCertificationDetail.setCreateBy(SecurityUtils.getUsername());
                    materialCertificationDetail.setCreateTime(new Date());
                    materialCertificationDetail.setMaterialCertificationId(materialCertificationApplication.getId());
                    materialCertificationDetail.setMinPurchaseQuantity(e.getMinPurchaseQuantity());
                    materialCertificationDetail.setMaterielId(e.getMaterielId());
                    materialCertificationDetail.setSupplierCode(e.getSupplierCode());
                    materialCertificationDetail.setSupplierName(e.getSupplierName());
                    return materialCertificationDetail;
                }).collect(Collectors.toList());
                materialCertificationDetailService.saveBatch(collect);
            }
        }
        return b;
    }

    /**
     * 获取物料认证申请 初始化信息
     *
     * @return
     */
    @Override
    public MaterialCertificationApplicationInitDto infoInit() {
        MaterialCertificationApplicationInitDto materialCertificationApplicationInitDto = new MaterialCertificationApplicationInitDto();
        String sn = feignService.getAuthorityCoding("warehouse:outbound:reworksto:list");
        if (sn == null) {
            throw new GlobalException("该单据未进行配置,无法生成单据编码");
        }
        materialCertificationApplicationInitDto.setCertificateNumber(sn);
        SysDept sysDept = sysDeptService.selectDeptById(SecurityUtils.getDeptId());
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        materialCertificationApplicationInitDto.setApplicationDept(sysDept.getDeptName());
        materialCertificationApplicationInitDto.setProposerBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        materialCertificationApplicationInitDto.setApplicationDate(new Date());
        materialCertificationApplicationInitDto.setContactInfo(sysUser.getPhonenumber());
        materialCertificationApplicationInitDto.setMaterialClassificationList(materialAwmateriaService.selectChildrenListThe2());

        return materialCertificationApplicationInitDto;
    }

    /**
     * 获取物料认证申请详细信息
     * @param id
     * @return
     */
    @Override
    public MaterialCertificationApplicationDetailsDto getInfoById(Long id){
        MaterialCertificationApplicationDetailsDto infoById = materialCertificationApplicationMapper.getInfoById(id);
        infoById.setMaterialCertificationDetailList(materialCertificationDetailService.getListByPid(id));
        return infoById;
    }

    /**
     * 审核
     * @return
     */
    @Transactional
    @Override
    public Boolean examine(MaterialCertificationApplicationVo materialCertificationApplicationVo){
        MaterialCertificationApplication materialCertificationApplication = new MaterialCertificationApplication();
        materialCertificationApplication.setUpdateBy(SecurityUtils.getUsername());
        materialCertificationApplication.setUpdateTime(new Date());
        materialCertificationApplication.setId(materialCertificationApplicationVo.getId());
        if(materialCertificationApplicationVo.getAuditState()==1){
            materialCertificationApplication.setStatus("3");
        }else if(materialCertificationApplicationVo.getAuditState()==2){
            materialCertificationApplication.setStatus("4");
        }
        boolean b = updateById(materialCertificationApplication);
        if(b){
            SysAuditHistoryTest sysAuditHistoryTest=new SysAuditHistoryTest();
            sysAuditHistoryTest.setAuditUserId(SecurityUtils.getUserId());
            sysAuditHistoryTest.setNodeId(materialCertificationApplicationVo.getId());
            sysAuditHistoryTest.setAuditState(materialCertificationApplicationVo.getAuditState());
            sysAuditHistoryTestMapper.insert(sysAuditHistoryTest);
        }
        return b;
    }
}
